﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MayhemWpf.UserControls;
using SensorHOL;
using Microsoft.WindowsAPICodePack.Sensors;
using SensorMayhem.Helpers;

namespace SensorMayhem.Wpf
{
    /// <summary>
    /// Interaction logic for LightConfig.xaml
    /// </summary>
    public partial class LightConfig : WpfConfiguration
    {

        SensorHelper<AmbientLightSensor, LuminousIntensity> _lightSensor;

        public LightConfig(int lightLevel, bool greater)
        {
            InitializeComponent();

            LightLevel = lightLevel;
            Greater = greater;

            try
            {
                CanSave = true;
                _lightSensor = new SensorHelper<AmbientLightSensor, LuminousIntensity>();
                _lightSensor.Initialize();
                _lightSensor.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_lightSensor_PropertyChanged);
            }
            catch (Exception)
            {
                CanSave = false;
                this.noSensorText.Visibility = System.Windows.Visibility.Visible;
            }
            
        }

        void _lightSensor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action( delegate()
                {
                    float currentIntensity = _lightSensor.Value.Intensity;
                    currentIntensityText.Text = currentIntensity.ToString() + " (" + LuxToCondition.ConvertLuxToCondition((int)currentIntensity) + ")";
                }));
        }

        public override string Title
        {
            get
            {
                return "Light sensor";
            }
        }

        public int LightLevel
        {
            get;
            set;
        }

        public bool Greater
        {
            get;
            set;
        }

        public override void OnLoad()
        {

            if (Greater)
            {
                aboveRadioButton.IsChecked = true;
            }
            else
            {
                belowRadioButton.IsChecked = true;
            }

            this.IntensitySlider.Value = Math.Log10(LightLevel);
            this.lightConditionText.Text = LuxToCondition.ConvertLuxToCondition(LightLevel);

        }

        public override void OnSave()
        {

            Greater = (bool)aboveRadioButton.IsChecked;
            LightLevel = SliderValueInLux();
        }

        private int SliderValueInLux()
        {
            int lux = 0;
            if (IntensitySlider.Value > 0)
            {
                lux = (int)Math.Pow(10, IntensitySlider.Value);
            }
            return lux;
        }

        private void IntensitySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int lux = SliderValueInLux();
            
            IntensityTextBlock.Text = lux.ToString();
            lightConditionText.Text = LuxToCondition.ConvertLuxToCondition(lux);
        }

    }
}
